import scipy.io as scio
from scipy.misc import imread,imshow
import tensorflow as tf
import numpy as np

data=scio.loadmat("/home/chen/DCNN_caffe-Tensorflow/test1.mat")


def weight_variable(name,shape):
    return data[name]
    '''
    initial=data[name]
    x = tf.sqrt(tf.cast(tf.shape(initial)[1], tf.float32))
    x=tf.cast(x,tf.int32)
    y = tf.shape(initial)[2]
    initial = tf.reshape(initial, [-1,x,x,y])
    initial = tf.transpose(initial, [2,1,0,3])
    initial=tf.cast(tf.reshape(initial,shape),tf.float32)
    return tf.Variable(initial)
    '''


def bias_variable(name,shape):
    #initial=tf.constant(data[name],shape)
     initial=data[name]
     initial=tf.cast(tf.reshape(initial,shape),tf.float32)
     return tf.Variable(initial)


def conv2d(x,W):
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
#[1,x_movement,y_movement,1], padding=SAME/VALID


def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
#[1,x_movement,y_movement,1], padding=SAME/VALID



def getImage(image_path,knn_path,closedform_path):
    ClosedForm_image=tf.cast(imread(closedform_path),tf.float32)
    KNN_image=tf.cast(imread(knn_path),tf.float32)
    x_image=tf.cast(imread(image_path),tf.float32)
    with tf.Session() as sess:
         s=x_image[:,:,0]*x_image[:,:,0]+x_image[:,:,1]*x_image[:,:,1]+x_image[:,:,2]*x_image[:,:,2]
         s=sess.run(s)
    s=tf.pack(s)
    x_image_r= tf.div(x_image[:,:,0],s)
    x_image_g= tf.div(x_image[:,:,1],s)
    x_image_b= tf.div(x_image[:,:,2],s)
    with tf.Session() as sess:
         ClosedForm_image=sess.run(ClosedForm_image)
         KNN_image=sess.run(KNN_image)
         # print(np.shape(sess.run(x_image_r)))
         #x_image=[sess.run(x_image_r),sess.run(x_image_g),sess.run(x_image_b)]
         x_image=[sess.run(x_image_r),sess.run(x_image_g),sess.run(x_image_b),ClosedForm_image, KNN_image]
         x_image = tf.pack(x_image,axis=2)
         x_image=sess.run(x_image)
         return x_image


image_path='/home/chen/DCNN_caffe-Tensorflow/image.png'
closedform_path='/home/chen/DCNN_caffe-Tensorflow/ClosedForm.png'
knn_path='/home/chen/DCNN_caffe-Tensorflow/KNN.png'
x_image=getImage(image_path,closedform_path,knn_path)
Image=x_image-0.5


Kernel = weight_variable('weights_conv1',[9,9,5,64])    #9x9 patch, in size 5, out size 64
#W_conv1=W_conv1[:,:,0:3,:]
biases=bias_variable('biases_conv1',[64])

x = tf.sqrt(tf.cast(tf.shape(Kernel)[1], tf.float32))
conv_patchsize=tf.cast(x,tf.int32)
conv_filters = tf.shape(Kernel)[2]
hei=tf.shape(Image)[0]
wid=tf.shape(Image)[1]
ch=tf.shape(Image)[2]
Kernel = tf.reshape(Kernel, [-1,conv_patchsize,conv_patchsize, conv_filters])
Kernel=tf.cast(Kernel,tf.float32)
hei=tf.cast(hei,tf.int32)
wid=tf.cast(wid,tf.int32)
conv_filters=tf.cast(conv_filters,tf.int32)
with tf.Session() as sess:
     hei=sess.run(hei)
     wid=sess.run(wid)
     conv_filters=sess.run(conv_filters)
     ch=sess.run(ch)
conv_data=np.zeros((hei, wid,  conv_filters))
for i in range(0,conv_filters-1):
    for j in range(0,ch-1):
        conv_subfilter=Kernel[j,:,:,i]
        conv_subfilter =tf.reshape(conv_subfilter, [conv_patchsize, conv_patchsize,1,1]);
        sub_image=Image[:,:,j]
        sub_image=tf.reshape(sub_image,[1,hei,wid,1])
        filter_data=tf.reshape(tf.nn.conv2d(sub_image,conv_subfilter,strides=[1,1,1,1],padding='SAME'),[hei,wid])       
        conv_seq_i=tf.cast(tf.pack(conv_data[:,:,i]),tf.float32)+tf.cast(filter_data,tf.float32)
    conv_array=conv_seq_i+biases[i]
conv_array=tf.unpack(conv_array)



print(conv_array)


















